//
//  ChatServerViewController.m
//  XXX
//
//  Created by Xiaoshiheng_pro on 2017/2/18.
//  Copyright © 2017年 Xiaoshiheng_pro. All rights reserved.
//

#import "ChatServerViewController.h"
#import "GCDAsyncSocket.h"
#import "Server.h"
#import "UIViewController+ImagePicker.h"

@interface ChatServerViewController ()<GCDAsyncSocketDelegate,ServerDelegate>

@property (weak, nonatomic) IBOutlet UITextField *messageTF;
@property (weak, nonatomic) IBOutlet UITextView *showMessageTF;
@property (weak, nonatomic) IBOutlet UIImageView *showMessageImage;

@property (nonatomic) Server  *Socket;

//服务器socket（开放端口，监听客户端socket的链接）
@property (nonatomic) GCDAsyncSocket *serverSocket;
//客户端socket
@property (nonatomic) GCDAsyncSocket *clinetSocket;

@end

@implementation ChatServerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.Socket = [Server ServerInit];
    self.Socket.delegate = self;
    
    self.serverSocket = self.Socket.serverSocket;
    self.clinetSocket = self.Socket.clinetSocket;
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)addImage:(id)sender {
    
    [self openChoiceWithCompleteBlock:^(NSDictionary *info){
        
        
        UIImage *image  = [info objectForKey:UIImagePickerControllerEditedImage];
        
        NSData *imageData = UIImagePNGRepresentation(image);
        NSData *sendingData = [self prepareData:imageData type:@"image"];
        //tag： 消息标记
        [self.clinetSocket writeData:sendingData withTimeout:-1 tag:0];
        
    }];
}

//发送消息
- (IBAction)sendMessageAction:(id)sender {
    
    NSData *data = [self.messageTF.text dataUsingEncoding:NSUTF8StringEncoding];
    NSData *sendingData = [self prepareData:data type:@"text"];
//    //withTimeout -1 :无穷大
//    //tag： 消息标记
    [self.clinetSocket writeData:sendingData withTimeout:-1 tag:0];

}
- (void)showMessageWithStr:(NSString *)str{
    self.showMessageTF.text = [self.showMessageTF.text stringByAppendingFormat:@"%@\n", str];
}


- (void)serverSocket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket{
 
    [self showMessageWithStr:@"链接成功"];
    [self showMessageWithStr:[NSString stringWithFormat:@"客户地址：%@ -端口： %d", newSocket.connectedHost, newSocket.connectedPort]];
    
    self.clinetSocket = self.Socket.clinetSocket;
}
- (void)serverSocketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
    
     [self showMessageWithStr:@"链接断开"];
}

- (void)serverSocket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    
    NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    if ([dic[@"type"] isEqualToString:@"text"]) {
        NSData *textData = [dic objectForKey:@"data"];
        NSString *text = [[NSString alloc] initWithData:textData encoding:NSUTF8StringEncoding];
        [self showMessageWithStr:text];
    }else{
        NSData *imageData = [dic objectForKey:@"data"];
        UIImage *img = [UIImage imageWithData:imageData];
        self.showMessageImage.image = img;
    }
    
}

-(void)serverSocket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
    
        NSLog(@"发送成功%ld",tag);
}


@end
